package com.zhq.apputil.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhq.apputil.base.BaseAdapterOnClickListener;
import com.zhq.apputil.utils.Utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


/***
 * 抽象适配器
 * */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int HAND_ITEM = 1;
    public static final int FOOTER_ITEM = 2;
    public static final int EMPTY_ITEM = 3;
    public List<T> dataList = new ArrayList<>();
    public BaseAdapterOnClickListener onClickListener;

    public abstract int getAdapterLayoutUrl(int viewType);

    public abstract View newLayout(@NonNull ViewGroup parent, int viewType, int resourceId);

    public abstract RecyclerView.ViewHolder getCreateHolder(View view, int viewType);

    public abstract void onBindBaseViewHolder(RecyclerView.ViewHolder parentHolder, int position);

    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public BaseRecyclerAdapter(Context mContext) {
        this.mContext = mContext;
        this.mLayoutInflater = LayoutInflater.from(mContext.getApplicationContext());
    }

    public Context getContext() {
        return mContext;
    }

    public LayoutInflater getLayoutInflater() {
        return mLayoutInflater;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setOnClickItemListener(BaseAdapterOnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getCreateHolder(newLayout(parent, viewType, getAdapterLayoutUrl(viewType)), viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        onBindBaseViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /***
     * 绑定数据源
     * @param list
     */
    public void bindDataList(Collection<T> list) {
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    /***
     * 分页处理
     * @param list
     */
    public void addAll(Collection<T> list) {
        int lastIndex = dataList.size();
        dataList.addAll(list);
        notifyItemRangeInserted(lastIndex, list.size());
    }

    /***
     * 移除数据源
     */
    public void removeDataListResource() {
        if (!Utils.isCollectionEmpty(dataList)) {
            dataList.clear();
        }
        dataList = null;
    }

    /****
     *
     * @param v
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setMargins(View v, int left, int top, int right, int bottom) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(left, top, right, bottom);
            v.setLayoutParams(p);
        }

    }


}
